<?php
namespace app\admin\controller\system;

use app\admin\logic\system\LoginLogic;
use app\admin\logic\system\MenuLogic;
use app\admin\model\model\system\SystemIcon;
use app\Request;
use think\App;

class MenuController extends BaseController
{
	public $title = '菜单';

	public function __construct(App $app)
	{
		parent::__construct($app);
	}

	// 表格页面
	public function index() {
		return $this->fetch();
	}

	// 获取全部数据以管理
	public function getDataAll() {
		$data = MenuLogic::getAll();
		return $this->setSuccess($data);
	}

	// 获取数据以展示
	public function getData(Request $request) {
        $accountId = LoginLogic::getId();
		$data = MenuLogic::getAllStatus($accountId);
        foreach($data as &$item) {
            $item['url'] = url($item['url'])->build();
        }
        if(isset($item)) unset($item);
		return $this->setSuccess($data);
	}

	// 添加
	public function add(Request $request) {
		if($post = $request->post()) {
			$res = MenuLogic::add($post);
			if($res) {
				return $this->setSuccess();
			} else {
				return $this->setError(\Debug::getDebug());
			}
		}

		$icon = SystemIcon::getAllForm();
		$this->assign('icon', $icon);

		return $this->fetch(['get'=>[]], 'form');
	}

	// 编辑
	public function update(Request $request) {
		if($post = $request->post()) {
			$res = MenuLogic::update($post);
			if($res === false) {
				return $this->setError(\Debug::getDebug());
			} else {
				return $this->setSuccess();
			}
		}

		$icon = SystemIcon::getAllForm();
		$this->assign('icon', $icon);

		$id = $request->get('id');
		$get = MenuLogic::getOne($id);

		return $this->fetch(['get'=>$get], 'form');
	}

	// 删除
	public function del(Request $request) {
		if(!$id = $request->get('id')) {
			return $this->setError('请提交要删除的数据');
		}

		$res = MenuLogic::del($id);
		if(!$res) {
			return $this->setError('删除失败');
		}
		return $this->setSuccess($res);
	}

	// 状态
	public function status(Request $request) {
		if(!$id = $request->get('id')) {
			return $this->setError('请选择要修改的数据');
		}
		$status = $request->get('status');
		$res = MenuLogic::status($id, $status);
		if(!$res) {
			return $this->setError('操作失败');
		}
		return $this->setSuccess();
	}

}
